package com.shey.qiuckpictest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.TextView;

import com.shey.adapter.ImagePagerAdapter;
import com.shey.base.BaseActivity;
import com.shey.fragment.ImageDetailFragment;
import com.shey.widget.HackyViewPager;

import java.util.ArrayList;

public class ImagePagerActivity extends BaseActivity {
    private static final String STATE_POSITION = "STATE_POSITION";
    public static final String IMAGE_INDEX = "imageIndex";
    public static final String IMAGE_LIST = "imageList";

    private HackyViewPager mPager;
    private ImagePagerAdapter mImagePagerAdapter;
    private int pagerPosition;
    private ArrayList<String> photoUrlList = null;

    @Override
    public void onCreate(Bundle savedInstanceState, View decorView) {

        enabledBackButton();

        pagerPosition = getIntent().getIntExtra(IMAGE_INDEX, 0);
        photoUrlList = getIntent().getStringArrayListExtra(IMAGE_LIST);

        if (photoUrlList == null || photoUrlList.size() <= 0) {
            toast(getString(R.string.data_transfer_error));
            finish();
            return;
        }

        mImagePagerAdapter = new ImagePagerAdapter(getSupportFragmentManager(), photoUrlList);
        mPager.setAdapter(mImagePagerAdapter);

        CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
        getSupportActionBar().setTitle(text);
        // 更新下标
        mPager.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                CharSequence text = getString(R.string.viewpager_indicator,arg0 + 1, mPager.getAdapter().getCount());
                getSupportActionBar().setTitle(text);
            }
        });
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }
        mPager.setCurrentItem(pagerPosition);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_imagepager;
    }

    @Override
    public void findView() {
        mToolBar.setTitleTextColor(getResources().getColor(R.color.white));
        mPager = (HackyViewPager) findViewById(R.id.imagepager_pager);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, mPager.getCurrentItem());
    }
}